<template>
  <!-- 注册页面的整体盒子 -->
  <div class="register-container">
    <!-- 注册的盒子 -->
    <div class="register-box">
      <!-- 标题的盒子 -->
      <div class="title-box"></div>
      <!-- 注册的表单区域 -->
      <el-form ref="formRef" :model="regForm" :rules="regFormRules">
        <el-form-item prop="username">
          <el-input v-model="regForm.username" placeholder="请输入用户名" prefix-icon="el-icon-user"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="regForm.password"
            placeholder="请输入密码"
            show-password
            prefix-icon="el-icon-lock"
          ></el-input>
        </el-form-item>
        <el-form-item prop="repassword">
          <el-input
            v-model="regForm.repassword"
            placeholder="请再次确认密码"
            show-password
            prefix-icon="el-icon-lock"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleReg">注册</el-button>
        </el-form-item>
        <el-link type="info" @click="$router.replace('/login')">去登陆</el-link>
      </el-form>
    </div>
  </div>
</template>

<script>
// 目标:渲染注册的表单结构
// 1.看文档,找对应的组件
// 2.把功能多的,全的标签+样式+数据,复制过来
// 3.把不用的多余的删掉,对照实际效果,进行修改
// 4.读文档,查询属性,事件表示什么意思
import { registerAPI } from '@/api/Register.js'
export default {
  name: 'Register',
  data() {
    // 自定义,确认密码的校验规则函数
    const validateRepass = (rule, value, callback) => {
      if (value !== this.regForm.password) {
        callback(new Error('两次密码不一致'))
      } else {
        callback()
      }
    }
    return {
      regForm: {
        // 尽量保持和后端接口字段一致
        username: '',
        password: '',
        repassword: ''
      },
      // 注册表单的校验规则对象
      regFormRules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { pattern: /^[a-z\d]{1,10}$/i, message: '用户名是1-10的字母数字组成', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { pattern: /^\S{6,15}$/, message: '密码必须是6-15位的非空字符', trigger: 'blur' }
        ],
        repassword: [
          { required: true, message: '请再次输入密码', trigger: 'blur' },
          { pattern: /^\S{6,15}$/, message: '密码必须是6-15位的非空字符', trigger: 'blur' },
          { validator: validateRepass, trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    // 注册
    handleReg() {
      // 整体校验
      this.$refs.formRef.validate(async valid => {
        if (!valid) return
        // 2.通过整体校验,
        console.log('ok')
        const {
          data: { code, message }
        } = await registerAPI(this.regForm)
        // 3.给个成功提示
        if (code === 0) {
          this.$message.success(message)
          // 4.注册成功需要跳转
          this.$router.push('/login')
        } else {
          // 5.注册失败,给个失败提示
          this.$message.warning(message)
        }
      })
    }
  }
}
</script>

<style lang="less" scoped>
.register-container {
  //注意.在样式中,要使用@路径对应的src的绝对路径,需要爱@前加 ~ 这样css才能识别@路径
  background: url('~@/assets/images/login_bg.jpg') center;
  background-size: cover;
  height: 100%;

  .register-box {
    width: 400px;
    height: 370px;
    background-color: #fff;
    border-radius: 3px;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    padding: 0 30px;
    box-sizing: border-box;

    .title-box {
      height: 60px;
      background: url('~@/assets/images/login_title.png') center no-repeat;
    }
  }
  .el-button {
    width: 100%;
  }
}
</style>
